#include <stdio.h>
#include <stdlib.h>
struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode *cur = head;
    struct ListNode *pre = NULL;
    while (cur != NULL)
    {
        if ((cur->val) == val) {
            if (cur == head) {
                head = cur->next;
                free(cur);
                cur = head;
            }
            else
            {
                pre->next = cur->next;
                free(cur);
                cur = pre->next;
            }
        }
        else
        {
            pre = cur;
            cur = cur->next;
        }
    }
}

int main()
{   struct ListNode* s1 = (struct ListNode* )malloc(sizeof(struct ListNode));
    struct ListNode* s2 = (struct ListNode* )malloc(sizeof(struct ListNode));
    struct ListNode* s3 = (struct ListNode* )malloc(sizeof(struct ListNode));
    s1->val = 1;
    s2->val = 2;
    s3->val = 3;
    s1->next = s2;
    s2->next = s3;
    s3->next = NULL;
    removeElements(s1,2);
    return 0;
}